using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using Common;
using CustomChart;
using Infragistics.UltraChart.Data.Series;
using Infragistics.UltraChart.Resources.Appearance;
using SExperiment;
using SExperiment.ExperimentResult;
using SProject.Paramter;
using SWellPlate;

namespace MainView.MainControls.Chart
{
	public class RawHRMCurveChart : BaseHRMCurveChart
	{
		private const string KeyStartTemperatureBefore = "StartTemperatureBefore";

		private const string KeyEndTemperatureBefore = "EndTemperatureBefore";

		private const string KeyStartTemperatureAfter = "StartTemperatureAfter";

		private const string KeyEndTemperatureAfter = "EndTemperatureAfter";

		public RawHRMCurveChart()
		{
			base.ThresholdVisible = true;
			base.Margin = new Thickness(1.0);
			base.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			base.VerticalAlignment = VerticalAlignment.Stretch;
			SetResourceReference(BaseCurveChart.CaptionProperty, "raw_melting_curve");
			SetResourceReference(BaseCurveChart.TitleXProperty, "Temperature");
			SetResourceReference(BaseCurveChart.TitleYProperty, "Rn");
			base.MinY = 0.0;
			base.MaxY = 1000.0;
			base.TitleXFormat = "0";
			base.TitleYFormat = "0.00";
		}

		public override string GetToolTipFormatX()
		{
			return "0.00";
		}

		public override Curve GetCurve(SampleTargetItem item)
		{
			if (item.Result is MeltingTargetResult)
			{
				return ((MeltingTargetResult)item.Result).RawMeltingCurve;
			}
			return null;
		}

		protected override void CreateThresholdSeries(BasicOptionItem item, ChartData chartData)
		{
			RegisterSubsetParamterEvent(chartData);
			List<XYSeriesEx> list = _ThresholdSeriesList.Where((XYSeriesEx s) => s.Tag == item).Cast<XYSeriesEx>().ToList();
			if (item != null && list.Count() == 0)
			{
				item.Event += base.BasicOptionItem_Event;
				if (item is HRMOptionItem)
				{
					double startTemperatureBefore = ((HRMOptionItem)item).StartTemperatureBefore;
					XYSeriesEx xYSeriesEx = new XYSeriesEx();
					xYSeriesEx.Visible = false;
					xYSeriesEx.PEs.Add(new PaintElement());
					xYSeriesEx.PEs[0].Fill = Color.Red;
					xYSeriesEx.Tag = item;
					xYSeriesEx.Key = "StartTemperatureBefore";
					xYSeriesEx.Points.Add(new XYDataPoint(startTemperatureBefore, base.MinY, "", false));
					xYSeriesEx.Points.Add(new XYDataPoint(startTemperatureBefore, base.MaxY, "", false));
					_ThresholdSeriesList.Add(xYSeriesEx);
					base.UltraChart.AddThresholdSeries(xYSeriesEx);
					list.Add(xYSeriesEx);
					startTemperatureBefore = ((HRMOptionItem)item).EndTemperatureBefore;
					xYSeriesEx = new XYSeriesEx();
					xYSeriesEx.Visible = false;
					xYSeriesEx.PEs.Add(new PaintElement());
					xYSeriesEx.PEs[0].Fill = Color.Red;
					xYSeriesEx.Tag = item;
					xYSeriesEx.Key = "EndTemperatureBefore";
					xYSeriesEx.Points.Add(new XYDataPoint(startTemperatureBefore, base.MinY, "", false));
					xYSeriesEx.Points.Add(new XYDataPoint(startTemperatureBefore, base.MaxY, "", false));
					_ThresholdSeriesList.Add(xYSeriesEx);
					base.UltraChart.AddThresholdSeries(xYSeriesEx);
					list.Add(xYSeriesEx);
					startTemperatureBefore = ((HRMOptionItem)item).StartTemperatureAfter;
					xYSeriesEx = new XYSeriesEx();
					xYSeriesEx.Visible = false;
					xYSeriesEx.PEs.Add(new PaintElement());
					xYSeriesEx.PEs[0].Fill = Color.Blue;
					xYSeriesEx.Tag = item;
					xYSeriesEx.Key = "StartTemperatureAfter";
					xYSeriesEx.Points.Add(new XYDataPoint(startTemperatureBefore, base.MinY, "", false));
					xYSeriesEx.Points.Add(new XYDataPoint(startTemperatureBefore, base.MaxY, "", false));
					_ThresholdSeriesList.Add(xYSeriesEx);
					base.UltraChart.AddThresholdSeries(xYSeriesEx);
					list.Add(xYSeriesEx);
					startTemperatureBefore = ((HRMOptionItem)item).EndTemperatureAfter;
					xYSeriesEx = new XYSeriesEx();
					xYSeriesEx.Visible = false;
					xYSeriesEx.PEs.Add(new PaintElement());
					xYSeriesEx.PEs[0].Fill = Color.Blue;
					xYSeriesEx.Tag = item;
					xYSeriesEx.Key = "EndTemperatureAfter";
					xYSeriesEx.Points.Add(new XYDataPoint(startTemperatureBefore, base.MinY, "", false));
					xYSeriesEx.Points.Add(new XYDataPoint(startTemperatureBefore, base.MaxY, "", false));
					_ThresholdSeriesList.Add(xYSeriesEx);
					base.UltraChart.AddThresholdSeries(xYSeriesEx);
					list.Add(xYSeriesEx);
				}
			}
		}

		protected override void ResetThresholdSeriesPoint()
		{
			foreach (XYSeriesEx thresholdSeries in _ThresholdSeriesList)
			{
				XYDataPointCollection points = thresholdSeries.Points;
				if (points.Count > 1 && (thresholdSeries.Key == "StartTemperatureBefore" || thresholdSeries.Key == "EndTemperatureBefore" || thresholdSeries.Key == "StartTemperatureAfter" || thresholdSeries.Key == "EndTemperatureAfter"))
				{
					points[0].ValueY = base.UltraChart.GetCurrentMinY();
					points[1].ValueY = base.UltraChart.GetCurrentMaxY();
				}
			}
		}

		protected override void RefreshThresholdValue(BasicOptionItem item, string property)
		{
			if (!(property == "StartTemperatureBefore") && !(property == "EndTemperatureBefore") && !(property == "StartTemperatureAfter") && !(property == "EndTemperatureAfter"))
			{
				return;
			}
			XYSeriesEx xYSeriesEx = _ThresholdSeriesList.Where((XYSeriesEx s) => s.Tag == item && s.Key == property).FirstOrDefault();
			if (xYSeriesEx == null)
			{
				return;
			}
			XYDataPointCollection points = xYSeriesEx.Points;
			for (int i = 0; i < points.Count; i++)
			{
				if (property == "StartTemperatureBefore")
				{
					points[i].ValueX = ((HRMOptionItem)item).StartTemperatureBefore;
				}
				else if (property == "EndTemperatureBefore")
				{
					points[i].ValueX = ((HRMOptionItem)item).EndTemperatureBefore;
				}
				else if (property == "StartTemperatureAfter")
				{
					points[i].ValueX = ((HRMOptionItem)item).StartTemperatureAfter;
				}
				else if (property == "EndTemperatureAfter")
				{
					points[i].ValueX = ((HRMOptionItem)item).EndTemperatureAfter;
				}
			}
			if (xYSeriesEx.Visible)
			{
				xYSeriesEx.Visible = false;
				xYSeriesEx.Visible = true;
			}
			RaiseItemUpdateEvent();
		}

		protected override string GetThresholdToolTips(XYSeriesEx series)
		{
			string result = base.GetThresholdToolTips(series);
			if (series.Tag is HRMOptionItem)
			{
				double d = double.NaN;
				HRMOptionItem hRMOptionItem = (HRMOptionItem)series.Tag;
				if (series.Key == "StartTemperatureBefore")
				{
					d = hRMOptionItem.StartTemperatureBefore;
				}
				else if (series.Key == "EndTemperatureBefore")
				{
					d = hRMOptionItem.EndTemperatureBefore;
				}
				else if (series.Key == "StartTemperatureAfter")
				{
					d = hRMOptionItem.StartTemperatureAfter;
				}
				else if (series.Key == "EndTemperatureAfter")
				{
					d = hRMOptionItem.EndTemperatureAfter;
				}
				if (!double.IsNaN(d))
				{
					result = hRMOptionItem.TargetName + Utility.GetMessage("Colon") + d.ToString("n1");
				}
			}
			return result;
		}

		protected override void SetThreshold(XYSeriesEx series, int x, int y)
		{
			if (!(series.Tag is HRMOptionItem))
			{
				return;
			}
			double locationXData = base.UltraChart.GetLocationXData(x);
			HRMOptionItem hRMOptionItem = (HRMOptionItem)series.Tag;
			if (series.Key == "StartTemperatureBefore")
			{
				if (locationXData < hRMOptionItem.EndTemperatureBefore)
				{
					hRMOptionItem.StartTemperatureBefore = locationXData;
				}
			}
			else if (series.Key == "EndTemperatureBefore")
			{
				if (locationXData > hRMOptionItem.StartTemperatureBefore && locationXData < hRMOptionItem.StartTemperatureAfter)
				{
					hRMOptionItem.EndTemperatureBefore = locationXData;
				}
			}
			else if (series.Key == "StartTemperatureAfter")
			{
				if (locationXData > hRMOptionItem.EndTemperatureBefore && locationXData < hRMOptionItem.EndTemperatureAfter)
				{
					hRMOptionItem.StartTemperatureAfter = locationXData;
				}
			}
			else if (series.Key == "EndTemperatureAfter" && locationXData > hRMOptionItem.StartTemperatureAfter)
			{
				hRMOptionItem.EndTemperatureAfter = locationXData;
			}
		}

		protected override Cursor GetThresholdDragCursor(string key)
		{
			switch (key)
			{
			case "StartTemperatureBefore":
			case "EndTemperatureBefore":
			case "StartTemperatureAfter":
			case "EndTemperatureAfter":
				return Cursors.VSplit;
			default:
				return GetThresholdDragCursor(key);
			}
		}

		protected override bool CanThresholdChanged(BasicOptionItem item)
		{
			return _ThresholdSeriesList.Where((XYSeriesEx s) => s.Visible).Count() == 4;
		}
	}
}
